/**
 * @(#) FrameworkUtil.java 
 */

package com.springcloud.sc.ucpv5.client.application.util;

import org.omg.CORBA.portable.ApplicationException;

import com.springcloud.sc.ucpv5.client.application.message.DataMessageManager;
import com.springcloud.sc.ucpv5.client.application.message.MessageService;
import com.springcloud.sc.ucpv5.client.application.message.MessageServiceException;
import com.springcloud.sc.ucpv5.client.application.message.MesssageHandle;
import com.springcloud.sc.ucpv5.client.commondata.SecuResult;
import com.springcloud.sc.ucpv5.client.commondata.data.LoginRsp;
import com.springcloud.sc.ucpv5.client.commondata.data.MsgHeader;
import com.springcloud.sc.ucpv5.client.module.datamanager.LoginDataManager;
import com.springcloud.sc.ucpv5.client.util.PublicUtil;

/**
 * Comments : Author : Create Date : 20010-4-11 Modification history : Sr Date
 * Modified By Why & What is modified
 * 
 * <pre>
 * 如：1. 2007.1.1 zhangsan  修改了addUser方法
 * </pre>
 * 
 * @version
 */

public final class FrameworkUtil {

	/**
	 * 握手线程停止标志
	 */
//	private static boolean shakeHandsStop;

	/**
	 * 登录状态
	 */
	private static boolean login;

	/**
	 * 
	 9-5
	 * 
	 * @return
	 * @see
	 */
	public static boolean isLogin() {
		return login;
	}


	public static int initService(String ipAddr, int port) {
		try {
			final int code = MessageService.getInstance().init(ipAddr, port);

			if (code == 0) {

			}
			return code;
		} catch (Exception e) {
			// log
		}

		return -1;
	}

	public static void destroy() {
		destroy(true);
	}

	/**
	 * 
	 4-14
	 * 
	 * @throws ApplicationException
	 * @see
	 */
	public static void destroy(boolean clearMsg) {

		MessageService messageService = (MessageService) MessageService.getInstance();

		try {
			if (messageService != null) {
				messageService.dispose();
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
		login = false;
	}

	public static boolean sysClose = false;

	public static void setCloseSystem() {
		sysClose = true;
		destroy();
	}
	/**
	 * 初始化CNMS，装载业务模块并启动自动锁屏监控线程
	 * 
	 * 5-29
	 * 
	 * @see
	 */
	public static boolean initCnmsSystem() {
		// 1.初始化连接

		int serverPort = Integer.valueOf(LoginDataManager.getInstance().getServerPort());
		String serverIp = LoginDataManager.getInstance().getServerIp();
		try {
			System.out.println("开始初始化连接");
			int retCode = MessageService.getInstance().init(serverIp, serverPort);
			System.out.println("初始化连接返回:" + retCode);
			if (SecuResult.RET_SUCCEED != retCode) {
				if (retCode != -1) {
				} else {
				}
				return false;
			}
		} catch (MessageServiceException e) {
			e.printStackTrace();
			System.out.println("初始化连接返回异常");
			return false;
		}
		SecuResult result = new SecuResult();
		System.out.println("开始登陆");

		String userName = LoginDataManager.getInstance().getName();
		String userPassword = LoginDataManager.getInstance().getPassWord();

		System.out.println("登录用户名:" + userName + "," + userPassword);
		LoginRsp rsp = DataMessageManager.getInstance().LoginSystem(userName, userPassword, result, true, true);
		if (rsp == null) {
			System.out.println("登陆失败 rsp Null");
			return false;
		}
		if (rsp.Result != SecuResult.RET_SUCCEED) {
			System.out.println("登陆失败:" + rsp.Result);
			return false;
		} else {
			System.out.println("登陆成功");
			String[] tmp = rsp.Auth_NodeID_List.split("##");
			String other = null;
			if (tmp.length == 2) {
				other = tmp[0];
			} else if (tmp.length == 1) {
				// 表示带其他和 不带FriendNodeList
				other = tmp[0];
			}
			String[] tmp1 = other.split("#");
			if (tmp1.length >= 1) {
				// 电话号码属于第一个
				LoginDataManager.getInstance().setUserTelphone(tmp1[0]);
			}
		}
		login = true;
		// 开始加载数据
		loadModule();
		return true;
	}

	/**
	 * 根据错误码获取错误信息
	 * 
	 * 7-31
	 * 
	 * @param errorCode
	 * @return
	 * @see
	 */
	public static String getErrorMessage(int errorCode) {
		return "";// ErrorCodeManager.getInstance().getErrorMessage(errorCode);
	}

	/**
	 * 绝对路径取得
	 * 
	 * @param relativePath
	 * @return
	 */
	public static String getAbsolutePath(String relativePath) {
		try {
			String path = "";
			return path;
		} catch (Exception e) {
			e.printStackTrace(System.err);
			// MainFramework.getInstance().log(FrameworkUtil.class.getName(), e,
			// "发生时间：" + PublicUtil.getNow(), Constant.LOG_LEVEL_ERROR,
			// Constant.MODULE_FRAMEWORK_ID);
			return null;
		}
	}


	/**
	 * 记录trace
	 * 
	 * 8-11
	 * 
	 * @param header
	 * @see
	 */
	public static void trace(String msg, int sessionId, MsgHeader header) {
		if (header.CmdID == Constant.CMD_COMMUNICATION_SHAKEHAND)
			return;

		String prefix = PublicUtil.nullToEmpty(FrameworkUtil.getAbsolutePath(Constant.TRACE_DIRECTORY_PATH));
		String tracePath = "";
		if (prefix.equals("")) {
			tracePath = "log" + Constant.FILE_SEPARATOR + Constant.TRACE_FILE_NAME;
		} else {
			tracePath = prefix + Constant.TRACE_FILE_NAME;// 设置路径
		}
	}

	/**
	 * 
	 * 4-13
	 *
	 * @throws ApplicationException
	 * @see
	 */
	public static boolean loadModule() {

		try {
			MesssageHandle.getInstance();

			Thread.currentThread().sleep(50);
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return true;
	}


}
